package pseudoAssemblerCompiler.twopassassembler.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Sash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pseudoAssemblerCompiler.twopassassembler.view.common.IView;

import java.lang.Object;


/**
 * Заполняет главное рабочее поле
 *
 * @author Чекашов Р. И.
 */
public class WorkSpace implements IView {

    public static final Logger LOGGER =
            LoggerFactory.getLogger(WorkSpace.class);

    /**
     * Основной контейнер рабочего пространства
     */
    private Composite _composite;

    /**
     * Левая передвижная перегородка
     */
    private Sash _topBottomSash_1;

    private SashForm _topBottomSashForm_2;

    private SashForm _leftRightSashForm_1;

    /**
     * Контейнер слева от _topBottomSash_1
     */
    private Composite _secondPassComposite;

    /**
     * Контейнер справа от _topBottomSash_1
     */
    private Composite _editorComposite;

    private Composite _differentTableComposite;

    private Composite _consoleOutputComposite;

    private Composite _rightContainerComposite;

    private Composite _firstPassComposite;

    public Composite get_composite() {
        return _composite;
    }

    public Composite get_secondPassComposite() {
        return _secondPassComposite;
    }

    public Composite get_editorComposite() {
        return _editorComposite;
    }

    public Composite get_differentTableComposite() {
        return _differentTableComposite;
    }

    public Composite get_consoleOutputComposite() {
        return _consoleOutputComposite;
    }

    public WorkSpace(Composite aParent) {
        _composite = new Composite(aParent, SWT.NONE);

        _topBottomSash_1 = new Sash(_composite, SWT.VERTICAL);
        _secondPassComposite = new Composite(_composite, SWT.NONE);
        _rightContainerComposite = new Composite(_composite, SWT.NONE);

        _leftRightSashForm_1 = new SashForm(_rightContainerComposite, SWT.VERTICAL);
        _firstPassComposite = new Composite(_leftRightSashForm_1, SWT.NONE);
        _consoleOutputComposite = new Composite(_leftRightSashForm_1, SWT.NONE);

        _topBottomSashForm_2 = new SashForm(_firstPassComposite, SWT.HORIZONTAL);
        _editorComposite = new Composite(_topBottomSashForm_2, SWT.NONE);
        _differentTableComposite = new Composite(_topBottomSashForm_2, SWT.NONE);

        listeners();
    }

    /**
     * Располагаем элементы в определённом порядке
     */
    private void layouts() {
        FormData data;

        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.top = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0);
        _composite.setLayout(new FormLayout());
        _composite.setLayoutData(data);

        //Sash - переплёт
        data = new FormData();
        data.top = new FormAttachment(0, 0); // Attach to top
        data.bottom = new FormAttachment(100, 0); // Attach to bottom
        data.left = new FormAttachment(0, 240); // Attach halfway across
        _topBottomSash_1.setLayoutData(data);

        //leftComposite - контейнер слева от leftSash
        data = new FormData();
        data.top = new FormAttachment(0, 0);
        data.bottom = new FormAttachment(100, 0);
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(_topBottomSash_1, 0);
        _secondPassComposite.setLayout(new FormLayout());
        _secondPassComposite.setLayoutData(data);

        //rightComposite - контейнер справа от leftSash
        data = new FormData();
        data.top = new FormAttachment(0, 0);
        data.bottom = new FormAttachment(100, 0);
        data.left = new FormAttachment(_topBottomSash_1, 0);
        data.right = new FormAttachment(100, 0);
        _rightContainerComposite.setLayout(new FormLayout());
        _rightContainerComposite.setLayoutData(data);

        //==========================================================
        //Sash - переплёт
        data = new FormData();
        data.left = new FormAttachment(0, 0); // Attach to top
        data.top = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0); // Attach to bottom
        _leftRightSashForm_1.setLayoutData(data);
        _leftRightSashForm_1.setWeights(new int[]{1, 2});

        //leftComposite - контейнер слева от leftSash
        data = new FormData();
        data.top = new FormAttachment(0, 0);
        data.bottom = new FormAttachment(_leftRightSashForm_1, 0);
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        _firstPassComposite.setLayout(new FormLayout());
        _firstPassComposite.setLayoutData(data);

        //rightComposite - контейнер справа от leftSash
        data = new FormData();
        data.top = new FormAttachment(_leftRightSashForm_1, 0);
        data.bottom = new FormAttachment(100, 0);
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        //data.height = 100;
        _consoleOutputComposite.setLayout(new FormLayout());
        _consoleOutputComposite.setLayoutData(data);

        //=============================================================

        //Sash - переплёт
        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.top = new FormAttachment(0, 0); // Attach to top
        data.right = new FormAttachment(100, 0); // Attach halfway across
        data.bottom = new FormAttachment(100, 0); // Attach to bottom
        _topBottomSashForm_2.setLayoutData(data);
        _topBottomSashForm_2.setWeights(new int[]{1, 2});

        //leftComposite - контейнер слева от leftSash
        data = new FormData();
        data.top = new FormAttachment(0, 0);
        data.left = new FormAttachment(0, 0);
        data.bottom = new FormAttachment(100, 0);
        data.right = new FormAttachment(_topBottomSashForm_2, 0);
        _editorComposite.setLayout(new FormLayout());
        _editorComposite.setLayoutData(data);

        //rightComposite - контейнер справа от leftSash
        data = new FormData();
        data.top = new FormAttachment(0, 0);
        data.left = new FormAttachment(_topBottomSashForm_2, 0);
        data.bottom = new FormAttachment(100, 0);
        data.right = new FormAttachment(100, 0);
        _differentTableComposite.setLayout(new FormLayout());
        _differentTableComposite.setLayoutData(data);
    }

    private void listeners() {
        _topBottomSash_1.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                // Reattach to the left edge, and use the x value of the event to
                // determine the offset from the left
                ((FormData) _topBottomSash_1.getLayoutData()).left = new FormAttachment(0, event.x);

                // Until the parent window does a layout, the sash will not be redrawn in
                // its new location. So, force a layout.
                _topBottomSash_1.getParent().layout();
            }
        });

//        _leftRightSashForm_1.addSelectionListener(new SelectionAdapter() {
//            public void widgetSelected(SelectionEvent event) {
//                // Reattach to the left edge, and use the x value of the event to
//                // determine the offset from the left
//                ((FormData) _leftRightSashForm_1.getLayoutData()).top = new FormAttachment(0, event.y);
//
//                // Until the parent window does a layout, the sash will not be redrawn in
//                // its new location. So, force a layout.
//                _leftRightSashForm_1.getParent().layout();
//            }
//        });

//        _topBottomSashForm_2.addSelectionListener(new SelectionAdapter() {
//            public void widgetSelected(SelectionEvent event) {
//                // Reattach to the left edge, and use the x value of the event to
//                // determine the offset from the left
//                ((FormData) _topBottomSashForm_2.getLayoutData()).right = new FormAttachment(0, event.x);
//
//                // Until the parent window does a layout, the sash will not be redrawn in
//                // its new location. So, force a layout.
//                _topBottomSashForm_2.getParent().layout();
//            }
//        });
    }

    @Override
    public void setLayoutData(Object layoutData) {
        _composite.setLayoutData(layoutData);
    }

    @Override
    public void show() {
        layouts();
    }

    @Override
    public Composite getComposite() {
        return _composite;
    }

    @Override
    public void setController(Object controller) {
        //this method do nothing
    }
}
